package com.zb.util;

import com.zb.entity.Item;
import com.zb.entity.UserOrder;
import com.zb.entity.vo.PurchaseHistoryVO;

import java.util.Objects;

/**
 * @Author lyx
 * @create 2021/9/7 10:29
 * @description:
 */
public class BeanUtil {
    /**
     * 避免循环引用导致生成json时递归栈溢出
     * @param order
     * @return
     */
    public static PurchaseHistoryVO copyPropertiesToVO(UserOrder order){
        if(Objects.isNull(order)){
            return null;
        }
        PurchaseHistoryVO purchaseHistoryVO = new PurchaseHistoryVO();
        purchaseHistoryVO.setId(order.getId());
        purchaseHistoryVO.setCreateTime(order.getCreateTime());
        purchaseHistoryVO.setDeleted(order.getDeleted());
        purchaseHistoryVO.setUpdateTime(order.getUpdateTime());
        Item item = order.getItem();
        if(Objects.nonNull(item)){
            purchaseHistoryVO.setItemId(item.getId());
            purchaseHistoryVO.setItemName(item.getItemName());
        }
        purchaseHistoryVO.setItemCount(order.getItemCount());
        purchaseHistoryVO.setTotalPrice(order.getTotalPrice());
        purchaseHistoryVO.setStatus(order.getStatus());
        return purchaseHistoryVO;
    }
}
